﻿package com.bestalbum.ui.controls{
	import com.bestalbum.ui.Main;
	import com.bestalbum.ui.PagerSection;
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.geom.Rectangle;
	import flash.text.TextField;

	public class Pager extends MovieClip{
		private var main:Main;
		private var mouseOveredSec:Number = -1;
		private var container:MovieClip;
		private var slider:MovieClip;
		private var sections:Array;
		private var sliderBounds:MovieClip;
		private var wd:Number;
		private var hg:Number;
		private var pages:Number;
		private var dx:Number = 1;
		private var dy:Number = 1;
		private var secWidth:Number;
		private var color:Number = 0xA76CAA;
		private var sliderAlpha:Number = 0.4;
		private var rectangle:Rectangle;
		public static var SECTION_SELECTED = "onSectionSelected";
		
		public function Pager(_main:Main, _x:Number, _y:Number, _width:Number, _height:Number, _pages:Number):void{
			super();
			this.main 	= _main;
			this.x 		= _x;
			this.y 		= _y;
			this.wd 	= _width;
			this.hg 	= _height;
			this.pages 	= _pages;
			sections = [];
			init();
		}
		
		private function init():void{
			container = new MovieClip();
			addChild(container);
			secWidth = Math.floor(wd/pages);
			for(var i:Number = 0; i <= pages; i++){
				var sec:PagerSection = new PagerSection(secWidth, hg, color, i.toString());
				sec.x = i * secWidth;
				sec.addEventListener(PagerSection.SECTION_CLICK, onSectionClick);
				container.addChild(sec);
				sections[sec.sectionNumber] = sec;
			}
			createSlider();
		}
		
		private function onSectionClick(event:Event):void{
			setSection(event.target.sectionNumber, event.target.x)
		}
		
		private function createSlider():void{
			slider = new MovieClip();
			addChild(slider);
			slider.buttonMode = true;
			slider.graphics.beginFill(color, sliderAlpha);
			slider.graphics.moveTo(0-dx,0-dy);
			slider.graphics.lineTo(secWidth+dx*2, 0-dy);
			slider.graphics.lineTo(secWidth+dx*2, hg+dy*2);
			slider.graphics.lineTo(0-dx, hg+dy*2);
			slider.graphics.lineTo(0-dx,0-dy);
			slider.graphics.endFill();
			rectangle = new Rectangle(0, 0, wd - slider.width/2, 0);
			slider.addEventListener(MouseEvent.MOUSE_DOWN, 	onDown);
			slider.addEventListener(MouseEvent.MOUSE_UP, 	onUp);
		}
		
		private function onDown(event:MouseEvent):void{
			var object:Object = event.target;
			object.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			object.startDrag(false, rectangle);
		}
		
		private function onUp(event:MouseEvent):void{
			var object:Object = event.target;
			object.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			object.stopDrag();
			checkSliderX();
		}
		
		private function checkSliderX(){
			var delta:Number = wd;
			var j:Number;
			for (var i:Number = 0; i < sections.length; i++){
				var deltaNew:Number = Math.abs(sections[i].x - slider.x);
				if(deltaNew < delta) {j = i; delta = deltaNew;}
			}
			var selSec:PagerSection = sections[j] as PagerSection;
			setSection(selSec.sectionNumber, selSec.x);
		}
		
		private function setSection(selectedSection:Number, sliderX:Number):void{
			trace("setSection() :: selectedSection="+selectedSection);
			main.addText("Pager::setSection() selectedSection="+selectedSection, true);
			slider.x = sliderX;
			markSection(selectedSection);
			var e:Event = new Event(Pager.SECTION_SELECTED);
            dispatchEvent(e);
		}
		
		private function markSection(sec:Number):void{
			trace("markSection() :: sec="+sec);
			var targetSec:PagerSection;
			if(mouseOveredSec >= 0){
				targetSec = sections[mouseOveredSec] as PagerSection;
				targetSec.restore();
			}
			targetSec = sections[sec] as PagerSection;
			targetSec.onSliderMove();
			mouseOveredSec = sec;
		}
		
		private function onMove(event:MouseEvent):void{
			try{
				var _txt:TextField = event.target.dropTarget as TextField;
				markSection(Number(_txt.text));
			} catch (e){
				trace("onMove() :: ERROR: "+e);
			}
		}
		
		
	}
}